<?php
class AdminDefaultStyleController extends ModuleAdminController 
{
    public function __construct() 
    {
        $this->table 	  = 'pt_default_style';
		$this->identifier = 'id_style';
        $this->className  = 'DefaultStyle';
		
		$this->context = Context::getContext();
		
		$this->addRowAction('view');
		$this->addRowAction('edit');
		$this->addRowAction('delete');
		
		$this->bulk_actions = array(
			'delete' => array(
				'text' => $this->l('Delete selected'), 
				'confirm' => $this->l('Delete selected items?')
			)
		);
        
        $this->fields_list = array(
			'id_style' => array(
				'title' => $this->l('ID'), 
				'align' => 'center', 
				'width' => 25
			),
            'id_cms' => array(
                'title' 	=> $this->l('CMS'),
                'callback' 	=> 'getCMSName'
            ),
            'selector' => array(
                'title' 	=> $this->l('CSS Selector'),
            ),
			'active' => array(
				'title' => $this->l('Displayed'), 
				'width' => 25, 
				'align' => 'center', 
				'active' => 'status', 
				'type' => 'bool', 
				'orderby' => false
			)
        );

        parent :: __construct();
    }
	
	public function renderForm()
	{
		$this->fields_form = array(
			'tinymce' => false,
			'legend' => array(
				'title' => $this->l('Default style'),
				'image' => '../img/admin/cog.gif'
			),
			'input' => array(
				array(
					'type' => 'select',
					'label' => $this->l('Prestatheme CMS'),
					'name' => 'id_cms',
					'required' => true,
					'options' => array(
						'query' => PrestathemeCms::getPrestathemeCMS(),
						'id' => 'id_cms',
						'name' => 'fullname'
					)
				),
				array(
					'type' => 'text',
					'label' => $this->l('Selector'),
					'name' => 'selector',
					'size' => 33,
					'required' => true,
					'hint' => $this->l('Invalid characters:').' <>;=#{}',
					'desc' => $this->l('A CSS selector')
				),
				array(
					'type' => 'textarea',
					'label' => $this->l('Style'),
					'name' => 'style',
					'rows' => 5,
					'cols' => 40,
					'hint' => $this->l('Forbidden characters:').' <>;=#{}',
					'desc' => $this->l('The CSS style associated to the selector')
				),
				array(
					'type' => 'radio',
					'label' => $this->l('Available'),
					'name' => 'active',
					'required' => false,
					'class' => 't',
					'is_bool' => true,
					'values' => array(
						array(
							'id' => 'active_on',
							'value' => 1,
							'label' => $this->l('Enabled')
						),
						array(
							'id' => 'active_off',
							'value' => 0,
							'label' => $this->l('Disabled')
						)
					),
				),
			),
			'submit' => array(
				'title' => $this->l('   Save   '),
				'class' => 'button'
			)
		);
        $languages = Language::getLanguages(false);
        $this->fields_value['lang_iso'] = array();
        foreach ($languages AS $lang)
        {
            $this->fields_value['lang_iso'][$lang['id_lang']] = $lang['iso_code'];
        }

		if (!($obj = $this->loadObject(true)))
			return;

		return parent::renderForm();
	}

	public function renderView()
	{
		if (!($object = $this->loadObject()))
			return;
			
		$this->tpl_view_vars = array(
			'id_lang' => Context::getContext()->language->id,
			'object' => $object,
			'head_title' => ($object->id ? (self::getCMSName($object->id_cms) .' : '. $object->selector) : $this->l('New Default Style')),
			'shopContext' => Shop::getContext(),
		);

		return parent::renderView();
	}

    public static function getCMSName ($id_cms)
    {
        $cms = new PrestathemeCms ($id_cms, Context::getContext()->language->id);
        if ($cms)
            return $cms->getCMSName();
        else
            return NULL;
    }
}